package com.bl.student.service.biz.goods.impl;

import com.bl.student.common.ApiResponseCode;
import com.bl.student.common.CommonServiceException;
import com.bl.student.common.enums.PointType;
import com.bl.student.common.utils.ListUtils;
import com.bl.student.common.utils.UUIDUtils;
import com.bl.student.dao.model.AddressEntity;
import com.bl.student.dao.model.GoodsEntity;
import com.bl.student.dao.model.TeacherEntity;
import com.bl.student.dao.model.UserEntity;
import com.bl.student.dao.repository.*;
import com.bl.student.service.biz.goods.GoodsService;
import com.bl.student.service.biz.order.OrderService;
import com.bl.student.service.biz.point.PointService;
import com.bl.student.service.biz.user.UserService;
import com.bl.student.service.vo.DelBody;
import com.bl.student.service.vo.DelListBody;
import com.bl.student.service.vo.goods.GoodsAddVo;
import com.bl.student.service.vo.goods.GoodsListVo;
import com.bl.student.service.vo.goods.GoodsModVo;
import com.bl.student.service.vo.purchase.PurchaseVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import java.util.Date;
import java.util.List;

/**
 * Created by liangweikang on 18/1/29.
 */
@Log4j2
@Service
public class GoodsServiceImpl implements GoodsService{

    @Autowired
    GoodsRepository goodsRepository;

    @Autowired
    PurchaseRepository purchaseRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    OrderService orderService;

    @Autowired
    AddressRepository addressRepository;

    @Autowired
    PointService pointService;

    @Autowired
    TeacherRepository teacherRepository;

    @Autowired
    UserService userService;


    @Override
    public GoodsListVo getAllGoodsList(Pageable pageable) {

        GoodsListVo goodsListVo = new GoodsListVo();

        Page<GoodsEntity> goodsEntities;

        goodsEntities = goodsRepository.findByOrderByWeightDesc(pageable);

        goodsListVo.setTotalCount(goodsEntities.getTotalElements());
        goodsListVo.setList(goodsEntities.getContent());

        return goodsListVo;
    }

    @Override
    public GoodsEntity getGoodDetail(String id) {
        GoodsEntity goodsEntity = new GoodsEntity();

        goodsEntity = goodsRepository.findOne(id);

        return goodsEntity;
    }

    @Override
    public void buyGoods(String uid, String type, PurchaseVo purchaseVo) {
        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity = goodsRepository.findOne(purchaseVo.getGoodsId());

        if (goodsEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应商品");
        }

        UserEntity userEntity = userRepository.findOne(uid);

        //账户口水不足
        if (!"1".equals(userEntity.getAuth())) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "用户未认证，无法购买商品");
        }

        //账户口水不足
        if (userEntity.getPoint() < goodsEntity.getPoint() * purchaseVo.getNum()) {
            throw new CommonServiceException(ApiResponseCode.NO_ENOUGH_MONEY.getCode(), "积分不足");
        }

        if (goodsEntity.getNum() < purchaseVo.getNum()) {
            throw new CommonServiceException(ApiResponseCode.NO_ENOUGH_MONEY.getCode(), "商品不足");
        }

        List<AddressEntity> addressEntities = addressRepository.findByPartybIdOrderByCreateTimeDesc(uid);

        if (addressEntities == null || addressEntities.size() == 0 ) {
            throw new CommonServiceException(ApiResponseCode.ADDRESS_INCORRECT.getCode(), "收货地址未填写");
        }

        //增加消费记录
        orderService.add(goodsEntity, userEntity.getId(), userEntity.getNickname(), userEntity.getMobile(), purchaseVo.getNum());

        // 甲方增加消费记录
        List<TeacherEntity> teacherEntityList = teacherRepository.findByOpenId(userEntity.getOpenId());
        TeacherEntity teacherEntity = ListUtils.getFirst(teacherEntityList);
        orderService.add(goodsEntity, teacherEntity.getId(), teacherEntity.getNickname(), teacherEntity.getMobile(), purchaseVo.getNum());


        //减少积分
        pointService.delPoint(userEntity, goodsEntity.getPoint() * purchaseVo.getNum());
//        userEntity.setPoint(userEntity.getPoint() - goodsEntity.getPoint() * purchaseVo.getNum());
//        userRepository.save(userEntity);

        //减少商品数量
        goodsEntity.setNum(goodsEntity.getNum() - purchaseVo.getNum());
        goodsRepository.save(goodsEntity);

        //积分记录
        pointService.addUserPoint(userEntity.getId(), PointType.BUY, goodsEntity.getPoint() * purchaseVo.getNum(), "1");
        // 甲方积分记录
        pointService.addUserPoint(teacherEntity.getId(), PointType.BUY, goodsEntity.getPoint() * purchaseVo.getNum(), "1");


    }

    @Override
    public void buyGoodsPartyA(String uid, String type, PurchaseVo purchaseVo) {
        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity = goodsRepository.findOne(purchaseVo.getGoodsId());

        if (goodsEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应商品");
        }

        TeacherEntity teacherEntity = teacherRepository.findOne(uid);

        //账户口水不足
        if (teacherEntity.getPoint() < goodsEntity.getPoint() * purchaseVo.getNum()) {
            throw new CommonServiceException(ApiResponseCode.NO_ENOUGH_MONEY.getCode(), "积分不足");
        }

        if (goodsEntity.getNum() < purchaseVo.getNum()) {
            throw new CommonServiceException(ApiResponseCode.NO_ENOUGH_MONEY.getCode(), "商品不足");
        }

        List<AddressEntity> addressEntities = addressRepository.findByPartybIdOrderByCreateTimeDesc(uid);

        if (addressEntities == null || addressEntities.size() == 0 ) {
            throw new CommonServiceException(ApiResponseCode.ADDRESS_INCORRECT.getCode(), "收货地址未填写");
        }

        //增加消费记录
        orderService.add(goodsEntity, teacherEntity.getId(), teacherEntity.getNickname(), teacherEntity.getMobile(), purchaseVo.getNum());

        // 乙方增加消费记录
        UserEntity userEntity = userService.getUserByOpenId(teacherEntity.getOpenId());
        orderService.add(goodsEntity, userEntity.getId(), userEntity.getNickname(), userEntity.getMobile(), purchaseVo.getNum());


        //减少积分
        pointService.delPointPartyA(teacherEntity, goodsEntity.getPoint() * purchaseVo.getNum());
//        userEntity.setPoint(userEntity.getPoint() - goodsEntity.getPoint() * purchaseVo.getNum());
//        userRepository.save(userEntity);

        //减少商品数量
        goodsEntity.setNum(goodsEntity.getNum() - purchaseVo.getNum());
        goodsRepository.save(goodsEntity);

        //积分记录
        pointService.addUserPoint(teacherEntity.getId(), PointType.BUY, goodsEntity.getPoint() * purchaseVo.getNum(), "1");
        // 乙方增加积分记录
        pointService.addUserPoint(userEntity.getId(), PointType.BUY, goodsEntity.getPoint() * purchaseVo.getNum(), "1");


    }

    @Override
    public void del(DelBody id) {
        goodsRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            goodsRepository.delete(id);
        }
    }

    @Override
    public void modify(GoodsModVo goodsModVo) {
        GoodsEntity goodsEntity = goodsRepository.findOne(goodsModVo.getId());

        if (goodsEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应商品信息");
        }
        goodsEntity.setDescription(goodsModVo.getDescription());
        goodsEntity.setImage(goodsModVo.getImage());
        goodsEntity.setPoint(goodsModVo.getPoint());
        goodsEntity.setNum(goodsModVo.getNum());
        goodsEntity.setRemark(goodsModVo.getRemark());
        goodsEntity.setStatus(goodsModVo.getStatus());
        goodsEntity.setTitle(goodsModVo.getTitle());
        goodsEntity.setLongImage(goodsModVo.getLongImage());
        goodsEntity.setUpdateTime(new Date());
        goodsEntity.setWeight(goodsModVo.getWeight());
        goodsRepository.save(goodsEntity);
    }

    @Override
    public void add(GoodsAddVo goodsAddVo) {
        GoodsEntity goodsEntity =  new GoodsEntity();
        goodsEntity.setCreateTime(new Date());
        goodsEntity.setDescription(goodsAddVo.getDescription());
        goodsEntity.setId(UUIDUtils.getUUID());
        goodsEntity.setImage(goodsAddVo.getImage());
        goodsEntity.setPoint(goodsAddVo.getPoint());
        goodsEntity.setNum(goodsAddVo.getNum());
        goodsEntity.setRemark(goodsAddVo.getRemark());
        goodsEntity.setStatus(goodsAddVo.getStatus());
        goodsEntity.setTitle(goodsAddVo.getTitle());
        goodsEntity.setUpdateTime(new Date());
        goodsEntity.setWeight(goodsAddVo.getWeight());
        goodsEntity.setLongImage(goodsAddVo.getLongImage());
        goodsRepository.save(goodsEntity);

    }
}
